/**
 * 
 */
package com.blogspot.radanddev.mbanksmsreader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * @author: Radoslaw Czwojdrak (Rad and Dev)
 */

public class Okienko extends Activity implements OnInitListener {

	private String Tresc_powiadomienia = "";
	private String Czysty_kod = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		String Operacja  = "";
		String full_sms_txt = "";
				
		Bundle extras = getIntent().getExtras();
		if (extras != null)
		{
			Tresc_powiadomienia = extras.getString("haslo");
			Operacja = extras.getString("operacja");
			full_sms_txt = extras.getString("full_sms");
			Czysty_kod = extras.getString("Czysty_kod").trim();		
		}

		
		
        TextView Info1 = (TextView)findViewById(R.id.info);        
        Info1.setText(Tresc_powiadomienia);

        TextView Info2 = (TextView)findViewById(R.id.operacja);        
        Info2.setText(Operacja);
        
        TextView full_sms = (TextView)findViewById(R.id.full_sms);        
        full_sms.setText(full_sms_txt);
        
        Button button1 = (Button)findViewById(R.id.Button1);        
        button1.setOnClickListener(mButton1);

        ImageButton button2 = (ImageButton)findViewById(R.id.Button2);        
        button2.setOnClickListener(mButton2);
                
        ImageButton button3 = (ImageButton)findViewById(R.id.Button3);        
        button3.setOnClickListener(mButton3);
	}

	
    private OnClickListener mButton1 = new OnClickListener()
    {
		@Override
		public void onClick(View arg0) {
			finish();	        
		}
    };
        
    private OnClickListener mButton2 = new OnClickListener()
    {
		@Override
		public void onClick(View arg0) {		
			
            AlertDialog.Builder alertbox = new AlertDialog.Builder(arg0.getContext());  
              
            alertbox.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface arg0, int arg1) {  
                }  
            });
            alertbox.setIcon(R.drawable.icon);
            alertbox.setTitle(R.string.about);
            alertbox.setMessage(R.string.info);
            alertbox.show();
		}
    };

	MediaPlayer mp = null;
	View okienko = null;
	char[] tabelka_literek = null;
	Boolean czyta = false;
	AudioManager am = null;
	int curentVolume = 0;
    
    private OnClickListener mButton3 = new OnClickListener()
    {
		@Override
		public void onClick(View arg0) {
			am = (AudioManager) getSystemService(arg0.getContext().AUDIO_SERVICE);
			curentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
			int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
			am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_SHOW_UI);
			
			okienko = arg0;
			tabelka_literek = Czysty_kod.toCharArray();
			if (!czyta)
				czytaj();
		}
    };
    
    int i = 0;
    private void czytaj() {
    	czyta = true;
		switch (Integer.parseInt(Character.toString(tabelka_literek[i])))
		{
			case 0: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m0);
				break;
			case 1: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m1);
				break;
			case 2: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m2);
				break;
			case 3: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m3);
				break;
			case 4: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m4);
				break;
			case 5: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m5);
				break;
			case 6: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m6);
				break;
			case 7: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m7);
				break;
			case 8: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m8);
				break;
			case 9: 
				mp = MediaPlayer.create(okienko.getContext(), R.raw.m9);
				break;					
		}
		mp.setVolume(100, 100);
        mp.start();
 
        mp.setOnCompletionListener(new OnCompletionListener() {
        	public void onCompletion(MediaPlayer arg0) 
            {
    			i++;
    			if (i < tabelka_literek.length)
    				czytaj();
    			else
    			{
    				i=0;
    				mp.release();
    				czyta = false;
    				am.setStreamVolume(AudioManager.STREAM_MUSIC, curentVolume, AudioManager.FLAG_SHOW_UI);
    			}
            }
        });

    }
	@Override
	public void onInit(int arg0) {
		// TODO Auto-generated method stub
		
	};

}
